<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用promise封装Ajax</title>
</head>
<body>
    <script>
        function ajax(url){
            return new Promise((resolve,reject) => {
                let xhr = new XMLHttpRequest();
                xhr.open("GET",url);
                xhr.send();
                xhr.onload = function(){
                    if(this.status == 200){
                        resolve(JSON.parse(this.response));
                    }else{
                        reject("加载失败");
                    }
                };
                xhr.onerror = function (){
                    reject(this);
                }
            })
        }

        let url = "https://localhost:8888/php";
        ajax(`${url}/user.php?username = 后盾人`)
        .then(user => ajax(`${url}/houdunren.php?id=${user.id}`))
        .then(lesson => console.log(lesson));
    </script>
</body>
</html>